$(function(){
	$("#functionNav").attr("href",navAddr);
	String.prototype.trim=function(){
	   return this.replace(/(^\s*)|(\s*$)/g, "");
	}
	
	//表格表头列
	var czlxCols;
	var orgId;
	
	var czrMap = {};
	
	var searchParams = $("#stat-form").serializeObject();
	var tableColumn = createColumn(searchParams);
	
	//初始化下拉列表
	initSelect(searchParams.sqbmid);
	
	//必须在initSelect()之后调用
	initTableHead();
	
	initTableFoot();
	
	createTable();
//	createTableWithOutPage('#stat-show', ProjectName+'/colstat.getOperationTotal.do', "month", tableColumn, searchParams);
	
	
	//统计按钮
	$('#stat-czy-query').click(function(){
		searchParams = $("#stat-form").serializeObject();
		tableColumn = createColumn(searchParams);
		if(!isValidStr(searchParams.czrid)){
			alertFunc("操作人不能为空！");
			return;
		}
		if(!isValidStr(searchParams.year)){
			alertFunc("统计年度不能为空！");
			return;
		}
		initTableHead();
		initTableFoot();
		createTable();
	});
	
	$('#stat-czy-export').click(function(){
		if(!isValidStr(searchParams.czrid)){
			alertFunc("请先统计再进行导出（操作人不能为空）！");
			return;
		}
		
		searchParams.title = $('#table-head').html();
		searchParams.author = userInfo.username;
		searchParams.type = 'czygzltj';
		
//		ajaxResult("get", false, ProjectName+'/colstat.export.do', searchParams);
		
		$.fileDownload(ProjectName+'/colstat.export.do', {
			data: $.param(searchParams),
			httpMethod: "post",
			successCallback: function (url) {
				console.log('文件下载成功！')
			},
			failCallback: function (responseHtml, url, error) { 
				console.log('文件下载失败！')
			}
		});
	});
	
	function createTable(){
		//导出按钮设为不可用
		$('#stat-czy-export').attr('disabled', true);
		
		$('#stat-show').bootstrapTable('destroy');		
		$('#stat-show').bootstrapTable({
			url: ProjectName+'/colstat.getOperationTotal.do',
			method: 'get',
			sidePagination: "server",
			queryParams: searchParams,
			columns: tableColumn,
			onLoadSuccess: function(d){  //加载成功时执行  
	             data=d;
	             //导出按钮可用
	             $('#stat-czy-export').attr('disabled', false);
	        }
		});
	};
	
	
	function isValidStr(str){
		if(str && str.trim().length>0)
			return true;
		return false;
	}
	
	function initSelect(){
		//统计年度下拉列表初始化
		var year = new Date().getFullYear();
		var arr = [];
		for(var i=2016; i<=year; i++){
			var map = {};
			map['key'] = i;
			map['value'] = i;
			arr.push(map);
		}
		loadSelect("#tjnd", arr,'value','key');
		$("#tjnd option[value="+year+"]").attr("selected", true);
		
		if(!orgId){
			var p = ajaxResult("get", false, ProjectName+"/root.getOrgInfoByField.do", {'key':'parentorgid', 'value':'0'});
			p = p.rows;
			//orgId = 'shandongbowuguan';是馆内所有申请人
			orgId = p[0].orgid;
		}
		var czrJson = ajaxResult("get", false, ProjectName+"/root.getUsersByOrg.do", {'orgId':orgId});
		loadSelect("#czr", czrJson.rows, 'userid', 'username');
		$.each(czrJson.rows, function(index, item){
			czrMap[item.userid] = item.username;
		});
	}
	
	function initTableHead(){
		var html = '操作员工作量统计';
		var con = '';
		var czlxJson = {'1':'登编', '2':'上传影像', '3':'分配库房', '4':'入库', '5':'出库验伤', 
				'6':'出库', '7':'退库验伤', '8':'退库', '9':'征集', '10':'移库',
				'11':'编目修改', '12':'注销藏品'};
		var year = new Date().getFullYear();
		for(var key in searchParams){
			var value = searchParams[key];
			
			if(!isValidStr(value+""))
				continue;
			
			if(key == 'czlx')
				con += '操作类型：'+czlxJson[value]+'；'
			if(key == 'year')
				con += '统计年度：'+value+'；'
			if(key == 'czrid')
				con += '操作人：'+czrMap[value]+'；'
				
			noSelected = false;
		}
		if(con)
			html += ' ( ' + con + ' )';
		$('#table-head').html(html);
	}
	
	function initTableFoot(){
		if(!userInfo)
			getUserRight();
		
		var content = "";
		content += '制表人：'+userInfo.username+'<br>';
		content += '统计时间：'+nowDate();
		
		$('#table-foot').html(content);
	}

	function createColumn(paras){
		var cols, tableName;
		var conditionOnX = isValidStr(paras.czlx);
		var conditionX = paras.czlx;
		
		if(!czlxCols){
			var tColumn = [];
			var firstCol = {};
			firstCol['field'] = 'month';
			firstCol['align'] = 'center';
			firstCol['title'] = '月份';
			tColumn.push(firstCol);
			
			var colNames = ['登编','上传影像','分配库房','入库','出库验伤','出库','退库验伤','退库', '征集', '移库', '编目修改', '注销藏品'];
			for(var i=1; i<=colNames.length; i++){
				map = {}
				map['field'] = i;
				map['title'] = colNames[i-1];
				map['align'] = 'center';
				tColumn.push(map);
			}
			czlxCols = tColumn;
		}

		if(conditionOnX)
			return filterColumn(conditionX, czlxCols);
		
		return czlxCols;
	}
	
	function filterColumn(conditionX, cols){
		var newCols = [];
		newCols.push(cols[0]);
		newCols.push(cols[conditionX]);
		
		return newCols;
	}
	
});